<template>
    <van-nav-bar
        title="我的收货地址"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
    />

    <!-- 从订单结算过来 -->
    <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onOrderSelect"
        v-if="order == 'order'"
    />

    <!-- 从我的过来 -->
    <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
        v-else
    />

</template>
<script>
export default {
    created() {
        let order = this.$route.query.order

        this.order = order

        this.AddressList()
    },
    data() {
        return {
            chosenAddressId:0,
            list:[],
            LoginUser:this.$cookies.get('LoginUser'),
            order:''
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        // 添加
        onAdd()
        {
            this.$router.push('/user/address/add')
        },
        // 编辑
        onEdit(item)
        {
            this.$router.push({path:'/user/address/edit',query:{addrid:item.id}})
        },
        // 异步查询
        async onSelect(item)
        {
            
            // 封装请求的数据
            let data = {
                id:item.id,
                userid:this.LoginUser.id
            }

            let result = await this.$api.select(data)

            if(result.code === 0)
            {
                this.$notify({
                    type: "danger",
                    message: result.msg,
                    duration:1500
                });
                return false
            }

            this.AddressList()
        },
        // 异步展示
        async AddressList()
        {
            // 请求后台获取某个用户的收货地址
            let result = await this.$api.AddressIndex({userid:this.LoginUser.id})

            // 获取到data数据并且赋值
            let data = result.data

            // 空数组
            let list = []

            // 当没数据提示
            if(data.length <= 0)
            {
                this.$notify({
                    type: "danger",
                    message: '暂无收货地址',
                    duration:1500
                });
                return false
            }

            // 把data里循环重组一个新的数据
            for(var item of data)
            {
                // 查找默认地址
                if(item.status == 1)
                {
                    this.chosenAddressId = item.id
                }

                // 重组数据
                let address = {
                    id:item.id,
                    name:item.consignee,
                    tel:item.mobile,
                    isDefault:item.status == 1 ? true : false
                }

                // 详细地址
                let info = ''

                if(item.provinces)
                {
                    info += `${item.provinces.name}-`
                }

                if(item.citys)
                {
                    info += `${item.citys.name}-`
                }

                if(item.districts)
                {
                    info += `${item.districts.name} `
                }

                if(item.address)
                {
                    info += `${item.address}`
                }

                // 追加
                address.address = info

                // 追加到数组
                list.push(address)
            }

            // 赋值
            this.list = list

        },

        onOrderSelect(item)
        {
            // console.log(item)
            let data = {
                id:item.id,
                consignee:item.name,
                mobile:item.tel
            }

            this.$cookies.set('AddressOrder',data)

            this.$router.go(-1)
        }
    },
}
</script>